<script setup>
import { introApi } from "@/services/index";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { nextTick, ref, watch, onMounted } from "vue";
import { appInfo, loginSwitch } from "@/services/user";
import { appCategory, brand } from "@/services/goods";
import Index from "../home/index.vue";

import { plan } from "@/services/stocktaking";
import { stockCounterStore } from "@/stores/stock";

const stockObj = stockCounterStore();

const form = ref(stockObj.form);
const list = ref(stockObj.selectDate ? JSON.parse(stockObj.selectDate) : []);
const showModal = ref(false);

onLoad((e) => {
  const topHeight = ref(uni.getStorageSync("topHight") || 0);
  code.value = e.code;
  // getList();
  getAppCategory();
});

const newCategoryList = ref([]);

const getAppCategory = async () => {
  try {
    let res = await brand(form.value);

    res.data.datas.forEach((item, index) => {
      item.select = false;
    });

    newCategoryList.value = res.data.datas;
    // newCategoryList.value = [];
  } catch (e) {
    console.log(e);
  }
};

const code = ref("");
const introData = ref([]);
const edit = ref(false);
const id = ref("");
const editName = () => {
  edit.value = true;
};

const getList = async () => {
  try {
    // 发送请求
    let res = await appInfo({});
    if (res.code == 200) {
      introData.value = res.data.employees;
      if (res.data.employees <= 0) {
        uni.redirectTo({
          url: "/pages/cashier/sales_order/index",
        });
      }
    }
  } catch (e) {
    uni.showToast({
      title: e.info,
      icon: "none",
    });
  }
};
// const selectFun = async (item) => {
//   uni.navigateTo({
//     url: `/pages/cashier/sales_order/index?ids=${
//       item.shopId
//     }&shopName=${decodeURIComponent(item.shopName)}`,
//   });
// };

const goadd = () => {
  uni.navigateTo({
    url: "/pages/cashier/sales_order/index",
  });
};
const onClickLeft = () => history.back();
const onClickRight = () => showToast("按钮");

const themeVars = ref({
  navBarIconColor: "#000000",
  navBarTextColor: "#000000",
  navBarBackground: "transparent",
});
const searchFun = () => {
  getAppCategory(1);
};

const openFun = (itemO, index, index2) => {
  let isOpen = itemO.open;

  newCategoryList.value.forEach((item, in1) => {
    if (in1 != index) {
      item.open = false;
    }
  });
  itemO.open = !isOpen;
};

const selectAll = ref(false);

const selectAllFun = () => {
  backFun(newCategoryList.value);

  selectAll.value = !selectAll.value;
};
const backFun = (list) => {
  list.forEach((item) => {
    item.select = !selectAll.value;
    if (item.children) {
      backFun(item.children);
    }
  });
};
const selectFun = (itemO, index, index2, index3) => {
  let isOpen = itemO.select;

  itemO.select = !isOpen;

  if (itemO.select) {
    let arr2 = newCategoryList.value.filter((element) => {
      return element.select == false;
    });
    if (arr2.length <= 0) {
      selectAll.value = true;
    }
  } else {
    selectAll.value = false;
  }
};
const planid = ref("");
//创建任务单
const isCerate = ref(false);
const goCerate = async () => {
  uni.showLoading({
    title: "提交中...",
  });
  if (isCerate.value) {
    return;
  }
  isCerate.value = true;
  form.value.bizIds = [];

  if (form.value.takeType == 1) {
    list.value.forEach((item) => {
      form.value.bizIds.push(item.id);
    });
  }
  if (form.value.takeType == 2) {
    list.value.forEach((item) => {
      form.value.bizIds.push(item.id);
    });
  }
  if (form.value.takeType == 3) {
    list.value.forEach((item) => {
      form.value.bizIds.push(item.id);
    });
  }
  console.log(form.value);
  // return;

  try {
    let res = await plan(form.value);
    if (res.code == 200) {
      showModal.value = true;
      planid.value = res.data.id;
      console.log(res, planid.value);
      uni.hideLoading();
      isCerate.value = false;
    } else {
      uni.hideLoading();
      uni.showToast({
        title: res.message,
        icon: "none",
      });
      isCerate.value = false;
    }
  } catch (e) {
    uni.hideLoading();
    isCerate.value = false;
    uni.showToast({
      title: e.message,
      icon: "none",
    });
  }
};
const goStock = () => {
  showModal.value = false;
  uni.reLaunch({
    url:
      "/pages/stocktaking/make/index?id=" +
      planid.value +
      "&name=" +
      encodeURIComponent(form.value.name),
  });
};

const goHome = () => {
  showModal.value = false;

  uni.reLaunch({ url: "/pages/index/index" });
};
//无网状态
const connected = () => {
  getAppCategory();
};
</script>

<template>
  <top-nav :title="'确认创建'" :border="false" rType="save"></top-nav>

  <!-- <view class="top" :style="'top: calc( 88rpx + ' + topHeight + 'px);'">
    <view class="search-k">
      <up-search
        shape="square"
        placeholder="品牌名称"
        v-model="form.name"
        :showAction="false"
        @search="searchFun"
      ></up-search>
    </view>
  </view> -->

  <view class="content_title"> 任务名称 </view>
  <view class="content_name">
    <view class="content_name_input" v-if="edit">
      <input
        class="input_to"
        type="text"
        v-model="form.name"
        placeholder="请输入任务名称"
      />
    </view>
    <view class="content_name_edit" v-if="!edit">
      <text>{{ form.name }}</text>
      <image
        @click="editName"
        src="@/static/images/edit.png"
        mode="scaleToFill"
      />
    </view>
  </view>

  <view class="content_title">
    盘点范围
    {{
      form.takeType == 0
        ? "(全场盘点)"
        : form.takeType == 1
        ? "(单品盘点)"
        : form.takeType == 2
        ? "(分类盘点)"
        : form.takeType == 3
        ? "(品牌盘点)"
        : ""
    }}
  </view>
  <!-- <view class="content_list" v-if="form.takeType == 0">
    <view class="content_name_edit">
      <text></text>
    </view>
  </view> -->
  <view class="content_list" v-if="form.takeType == 1">
    <view class="content_name_edit" v-for="(item, index) in list" :key="index">
      <text>{{ item.name }}</text>
      <text class="content_name_edit_num">{{ item.stockNum }}</text>
    </view>
  </view>
  <view class="content_list" v-if="form.takeType == 2">
    <view class="content_name_edit" v-for="(item, index) in list" :key="index">
      <text>{{ item.name || item.categoryName }}</text>
      <text class="content_name_edit_num">{{ item.count }}</text>
    </view>
  </view>
  <view class="content_list" v-if="form.takeType == 3">
    <view class="content_name_edit" v-for="(item, index) in list" :key="index">
      <text>{{ item.name }}</text>
    </view>
  </view>

  <view class="goods_bt">
    <view class="goods_bt_to">
      <!-- <view class="bt_left" @click="selectAllFun">
        <view class="select" :class="selectAll ? 'select2' : ''">
          <image
            v-if="selectAll"
            src="@/static/images/select.png"
            mode="scaleToFill"
          />
        </view>
        <text>全选</text>
      </view> -->

      <up-button
        class="bt_right"
        @click="goCerate"
        type="primary"
        color="#1679FB"
        text="确认创建"
      ></up-button>
    </view>
  </view>

  <up-modal
    width="600rpx"
    :showCancelButton="true"
    cancelText="返回首页"
    confirmText="去盘点"
    :show="showModal"
    :title="'创建成功了，可以去盘点了'"
    @confirm="goStock"
    @cancel="goHome"
  ></up-modal>
  <noWork @connected="connected"> </noWork>
</template>

<style lang="scss" scoped>
@import "index.scss";
</style>
